package org.crackjack.util.http;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.crackjack.model.Appointment;
import org.crackjack.model.Profile;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class HttpClient {

    private RestTemplate restTemplate;

    public List requestList(String type) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<String>(headers);
        ResponseEntity<List> response = restTemplate.exchange("http://localhost:8080/app-services-rest/{type}/list", HttpMethod.GET,
                entity, List.class, type);
        return response.getBody();
    }

    public Appointment requestAppointment(Long id) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<String>(headers);
        ResponseEntity<Appointment> response = restTemplate.exchange("http:/localhost:8080/app-services-rest/appointment/get/{id}",
                HttpMethod.GET, entity, Appointment.class, id);
        return response.getBody();
    }

    public Appointment requestProfile(Long id) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<String>(headers);
        ResponseEntity<Appointment> response = restTemplate.exchange("http:/localhost:8080/app-services-rest/appointment/get/{id}",
                HttpMethod.GET, entity, Appointment.class, id);
        return response.getBody();
    }

    public void update(Object object, String type) {
        restTemplate.put("http:/localhost:8080/app-services-rest/{type}/update", object, type);
    }

    public void delete(Long id, String type) {
        restTemplate.delete("http:/localhost:8080/app-services-rest/{type}/delete/{id}", type, id);
    }

    public Appointment createAppointment(Appointment appointment) {
        return restTemplate.postForObject("http:/localhost:8080/app-services-rest/appointment/create", appointment, Appointment.class);

    }

    public Profile createProfile(Profile profile) {
        return restTemplate.postForObject("http:/localhost:8080/app-services-rest/profile/create", profile, Profile.class);

    }

    public Map authorization(String user, String password) throws JsonGenerationException, JsonMappingException, IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> map = new HashMap<String, String>();
        map.put("user", user);
        map.put("password", password);
        String json = mapper.writeValueAsString(map);

        HttpEntity<String> entity = new HttpEntity<String>(json, headers);

        ResponseEntity<Map> response = restTemplate.exchange("http:/localhost:8080/app-services-rest/authorization", HttpMethod.GET,
                entity, Map.class);
        return response.getBody();
    }

    public RestTemplate getRestTemplate() {
        return restTemplate;
    }

    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

}
